package com.stylefeng.guns.modular.flagglory.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.stylefeng.guns.core.base.controller.BaseController;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import com.stylefeng.guns.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import com.stylefeng.guns.common.persistence.model.Level;
import com.stylefeng.guns.modular.flagglory.service.ILevelService;

import javax.validation.Valid;

/**
 * 等级控制器
 *
 * @author fengshuonan
 * @Date 2018-01-31 14:14:46
 */
@Controller
@RequestMapping("/level")
public class LevelController extends BaseController {

    private String PREFIX = "/flagGlory/level/";

    @Autowired
    private ILevelService levelService;

    /**
     * 跳转到等级首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "level.html";
    }

    /**
     * 跳转到添加等级
     */
    @RequestMapping("/level_add")
    public String levelAdd() {
        return PREFIX + "level_add.html";
    }

    /**
     * 跳转到修改等级
     */
    @RequestMapping("/level_update/{levelId}")
    public String levelUpdate(@PathVariable Integer levelId, Model model) {
        Level level = levelService.selectById(levelId);
        model.addAttribute("item",level);
        LogObjectHolder.me().set(level);
        return PREFIX + "level_edit.html";
    }

    /**
     * 获取等级列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        return levelService.selectList(null);
    }

    /**
     * 新增等级
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(@Valid Level level, BindingResult result) throws Exception {
        if (result.hasErrors()) {
            throw new Exception(result.getFieldError().getDefaultMessage());
        }
        levelService.insert(level);
        return super.SUCCESS_TIP;
    }

    /**
     * 删除等级
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer levelId)throws Exception {
        throw new Exception("暂时不让删！");
//        levelService.deleteById(levelId);
//        return SUCCESS_TIP;
    }

    /**
     * 修改等级
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(@Valid Level level, BindingResult result) throws Exception {
        if (result.hasErrors()) {
            throw new Exception(result.getFieldError().getDefaultMessage());
        }
        levelService.updateById(level);
        return super.SUCCESS_TIP;
    }

    /**
     * 等级详情
     */
    @RequestMapping(value = "/detail/{levelId}")
    @ResponseBody
    public Object detail(@PathVariable("levelId") Integer levelId) {
        return levelService.selectById(levelId);
    }
}
